//
//  mainView.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
//  http://appsearch.sagetouch.com/app123/ipadappinfo/391139903.html
//  

#import "mainView.h"
#import "commondef.h"

//#import "XMLRPCResponse.h"
//#import "XMLRPCRequest.h"
//#import "XMLRPCConnection.h"

#import "NSString+URLEncoding.h"

#import "SBJson.h"

@implementation mainView

static int  isorttp = NEW_PBT;

@synthesize delegate;

- (void)dealloc
{
    [navBar release];
    [tableview release];
    [results release];
    [webdatabuf release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        
        navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, screenrc.size.width, NAV_HEIGHT)];
        
        [self addSubview: navBar];
        
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @""];
        UIBarButtonItem *rightbtn = [[UIBarButtonItem alloc] initWithTitle:@"sort" 
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self 
                                                                     action:@selector(sort:)];
        navItem.rightBarButtonItem = rightbtn;
        [navBar pushNavigationItem:navItem animated:YES];
        [navItem release];
        [rightbtn release];
        
        tableview = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT, 
                                                                  screenrc.size.width,
                                                                  screenrc.size.height - NAV_HEIGHT - BTN_HEIGHT)];
        [self addSubview:tableview];
        [tableview setDelegate:self];
        [tableview setDataSource:self];
        tableview.separatorColor = [UIColor clearColor];
        
        webdatabuf = [[NSMutableData alloc] initWithLength:0];
        
        [self ConnectionBySort:isorttp];
//        NSString *urlString = [NSString stringWithFormat:@"http://www.mwinphone.com/api/get_recent_posts/"];
        
    }
    return self;
}

-(void)sort:(id)sender
{
    [self setFitFrame:FULL_FF];
    
    [delegate IsEnableBtn:NO];
    
    CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
    popsortview = [[popsortView alloc] initWithFrame:screenrc];
    popsortview.alpha = 0.5;
    popsortview.backgroundColor = [UIColor whiteColor]; 
    popsortview.delegate = self;
    [popsortview setSortType:isorttp];
    [self addSubview:popsortview];
}

-(void)sortby:(int)sorttype
{
    [self setFitFrame:NOMAL_FF];
    
    [popsortview removeFromSuperview];
    
    [delegate IsEnableBtn:YES];
    
    [self ConnectionBySort:sorttype];
   
    if(sorttype != CANCEL_PBT)
        isorttp = sorttype;
}

#pragma mark- connect
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [webdatabuf appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConn
{
    [connection release];
	NSString *jsonString = [[NSString alloc] initWithData:webdatabuf encoding:NSUTF8StringEncoding];
    
    [webdatabuf release];
    webdatabuf = [[NSMutableData alloc] initWithLength:0];
    
    [results release];
	results = [[jsonString JSONValue] retain]; 
    [jsonString release];
    
    [tableview reloadData];
}

#pragma mark-table data method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TVC_HEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    NSString *identfier = [[results objectAtIndex:[indexPath indexAtPosition:1]] objectForKey:@"Ti"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
    
    if(cell == nil)
    {
        cell = [[mainViewTableCall alloc]initWithDictionary:[results objectAtIndex:[indexPath indexAtPosition:1]] 
                                            reuseIdentifier:identfier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        ((mainViewTableCall*)cell).delegate = self;
    }
    return  cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [results count];
}

#pragma mark -func
-(void)ConnectionBySort:(int) sorttp
{
    NSString  *strurl = nil;
    
    switch (sorttp) {
        case GARBAGE_PBT:
            strurl = [NSString stringWithFormat:@"http://appsearch.sagetouch.com/app123/iphoneservice/6-0-1-0-0.html"];
            break;
        case POPULAR_PBT:
            strurl = [NSString stringWithFormat:@"http://appsearch.sagetouch.com/app123/iphoneservice/5-0-1-0-0.html"];
            break;
        case NEW_PBT:
            strurl = [NSString stringWithFormat:@"http://appsearch.sagetouch.com/app123/iphoneservice/4-0-1-0-0.html"];
            break;
        case CANCEL_PBT:
            return;
            break;
        default:
            break;
    } 
    
    NSURL *url = [NSURL URLWithString:strurl];    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        
    [request release];
}

#pragma mark -detailsDelegate
-(void)detailback
{
    [self setFitFrame:NOMAL_FF];
    
    [delegate IsEnableBtn:YES];
    
    [detailsview removeFromSuperview];
    [detailsview release];
}

#pragma mark -mainviewTCDelegate
-(void)showDetails:(NSString *)strurl
{
    [self setFitFrame:FULL_FF];
    
    [delegate IsEnableBtn:NO];
    
    detailsview = [[detailsView alloc] initWithUrl:strurl];
    detailsview.delegate = self;
    [self addSubview:detailsview];
}

-(void)setFitFrame:(int)fitTp
{
    CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
    screenrc.origin.y = 0;
    switch (fitTp) {
        case NOMAL_FF:
        {
            float btnheight = screenrc.size.height - BTN_HEIGHT;
            self.frame = CGRectMake(0.0, 0.0, screenrc.size.width, btnheight);
        }
            break;
        case FULL_FF:
            self.frame = screenrc;
            break;
        default:
            break;
    }
}
@end
